package com.xywl.escort.view.pwd

import android.os.Bundle
import android.text.TextUtils
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.xywl.escort.R
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.bean.MessageEvent
import com.xywl.escort.common.Constant
import kotlinx.android.synthetic.main.activity_setting_pay_pwd.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import timber.log.Timber

@Route(path = "/xy/pwd/setting/pay")
class SettingPayPwdActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting_pay_pwd)
        showBackButton()
        setTitleText(R.string.setting_set_pay_pwd)

        text_pay_pwd.text = getString(R.string.setting_set_pay_pwd)
        edit_pwd.setOnPasswordChangeListener { password ->
            if (password.length == 6) {
                ARouter.getInstance().build("/xy/pwd/confirm/pay").withString("pwd", password).navigation()
            }
        }

        btn_next.setOnClickListener {
            val pwd = edit_pwd.text.toString().trim()
            Timber.e("pwd=$pwd")
            if (pwd.length == 6) {
                ARouter.getInstance().build("/xy/pwd/confirm/pay").withString("pwd", pwd).navigation()
            }
        }

        EventBus.getDefault().register(this)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun mainEventBus(event: MessageEvent) {
        Timber.tag("SettingPayPwdActivity").e("event.message=" + event.message)
        Timber.tag("SettingPayPwdActivity").e("event.type=" + event.type)
        when (event.type) {
            Constant.EventType.EVENT_SET_PAY_PWD_TYPE -> {
                val message = event.message
                if (!TextUtils.isEmpty(message)) {
                    if (message == Constant.EventMessage.FINISH_ACTIVITY) {
                        finish()
                    }
                }
            }
            else -> {
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}
